# Create distance matrices and datasets for matching

library(here)

source(here("Design", "nonbimatchingfunctions.R"))

load(here("Data", "wrkdatOwnMap_new.rda"), verbose = TRUE)
# thecovs <- c("csd.pop", "vm.csd", "community_area_km")

## Restrict matching to people with value map perceptions
wrkdatOwnMap_new <- droplevels(wrkdatOwnMap_new[!is.na(wrkdatOwnMap_new$vm.community.norm2), ])
# summary(wrkdatOwnMap_new[, c(thecovs, "vm.norm2", "vm.community.norm2")])

## Make simple objective distance
vmdaDist <- scalar.dist(wrkdatOwnMap_new$da_prop_vm_20pct_06, scalefactor = 100000) ## the matching software wants integers
dimnames(vmdaDist) <- list(row.names(wrkdatOwnMap_new), row.names(wrkdatOwnMap_new))

## Test to ensure that order of people in matrices is the same
stopifnot(all.equal(row.names(wrkdatOwnMap_new), row.names(vmdaDist)))
## all.equal(row.names(wrkdatOwnMap_new),row.names(mhRankDist01))

save(vmdaDist, file = here("Design", "dist_mats_anyDA_new.rda"))
save(wrkdatOwnMap_new, file = here("Data", "wrkdatOwnMap_anyDA_new.rda"))

system("touch Design/dist_mats_data_anyDA_new.done")
